﻿using Ebusiness_UserService.Data;
using Ebusiness_UserService.Model;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Ebusiness_UserService.Repositorys
{
    /// <summary>
    /// 
    /// </summary>
    [Dependency(ServiceLifetime.Transient)]
    public class UserLoginHistoryRepository : EfCoreRepository<Ebusiness_UserServiceDbContext, UserLoginHistory, Guid>, IUserLoginHistoryRepository
    {
        public UserLoginHistoryRepository(IDbContextProvider<Ebusiness_UserServiceDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }

        public async Task AddNewLoginHistoryAsync(PhoneNumber phoneNumber, string msg)
        {
            Ebusiness_UserServiceDbContext ebusiness_UserServiceDbContext = await GetDbContextAsync();
            //Console.WriteLine("wwwwwwwwwwwwwwwww" + ebusiness_UserServiceDbContext.GetHashCode());
            //var data = ebusiness_UserServiceDbContext.GetHashCode(); //19063697 64096642
            //await Task.Delay(6000);

            User? user = await ebusiness_UserServiceDbContext.users.Where(x => x.phoneNumber.Phone == phoneNumber.Phone).FirstOrDefaultAsync();
            Guid? userid = null;
            if (user != null)
            {
                userid = user.Id;
            }
            //添加历史记录
            await InsertAsync(new UserLoginHistory(userid, phoneNumber, msg));

            //throw new Exception("失败失败失败失败失败失败失败");
        }
    }
}
